package com.wonders.ssh.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.Session;
import org.junit.Test;

import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.wonders.ssh.model.User;
import com.wonders.ssh.service.UserService;

/**
 * liubiao
 */
public class UserAction extends ActionSupport implements RequestAware {
	private UserService userService;
	private User user = new User();
	private int id;
	private String username;
	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	private String password;
	private List list;

	public String login() {
		System.out.println("username:" + username);
		System.out.println("password:" + password);
		if (username != null && !"".equals(username)) {
			System.out.println("++");
			List list = this.userService.findByUsername(username);
			if (list != null && list.size() > 0) {
				System.out.println("--");
				User user = (User) list.get(0);
				if (user.getPassword().equals(password)) {
					System.out.println("**");
					return "success";
				}
			}
			System.out.println("//");
		}
		return "login";
	}

	public String add() {
		user.setUsername(username);
		user.setPassword(password);
		System.out.println(user.getId()+"--"+user.getUsername()+"--"+user.getPassword());
		userService.save(user);
		return "add";
	}

	public String delete() {
		User user = userService.findById(id);
		userService.delete(user);
		return "delete";
	}

	public String findById() {
		User user=userService.findById(id);
		
		return "findById";
	}

	public String findByUsername() {
		list=userService.findByUsername(username);
		
		return "findByUsername";

	}

	public String findAll() {
		List list=userService.findAll();
		request.put("list1", list);
		return "findAll";
	};

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	private Map<String, Object> request;
	
	public void setRequest(Map<String, Object> request) {
		this.request=request;
		
	}
}
